
const {database} = require("@common/config")
class Middleware {
  async selectUserInfo(userId){
    try {
      const statement = `
    SELECT 
    u.id,u.name,u.cellphone,u.createAt,u.updateAt,
    JSON_ARRAYAGG(JSON_OBJECT('id',r.id,'intro',r.intro,'name',r.name,'createAt',r.createAt,'updateAt',r.updateAt)) as roles,
    JSON_ARRAYAGG(JSON_OBJECT('id',d.id,'name',d.name,'parentId',d.parentId,'leader',d.leader,'createAt',d.createAt,'updateAt',d.updateAt)) as department
     FROM user as u
     INNER JOIN user_role as ur ON u.id = ur.user_id
     INNER JOIN role as r ON r.id = ur.role_id
     INNER JOIN user_department as ud ON u.id = ud.user_id
     INNER JOIN department as d ON d.id = ud.department_id
     WHERE u.id = ?
    `
    const [result] = await database.promise().execute(statement,[userId])
    return result[0]
    } catch (error) {
      console.log(error);
    }
  }
}

module.exports = new Middleware()